﻿using System.Text.Json.Serialization;
using DimensionsHelper.Serialization.CommandLine;

namespace DimensionsHelper.CommandLine.PreTableRender;

[JsonConverter(typeof(JsonStringEnumConverter<SortOrders>))]
public enum SortOrders
{
    /// <summary>
    ///     不排序
    /// </summary>
    None,

    /// <summary>
    ///     升序排列
    /// </summary>
    Asc,

    /// <summary>
    ///     降序排列
    /// </summary>
    Desc
}


[Serializable]
public class PreFieldOptions
{
    /// <summary>
    ///     配置名，替换占位符时使用
    /// </summary>
    public string Name { get; set; } = string.Empty;

    /// <summary>
    ///     字段名
    /// </summary>
    public string FieldName { get; set; } = string.Empty;

    /// <summary>
    ///     替换模板时使用码号中的末尾数字部分
    /// </summary>
    public bool NumberCode { get; set; }

    /// <summary>
    ///     字段单独使用的表头表达式，优先级高于<see cref="PreTableOptions.HeaderExpression" />。
    /// </summary>
    public string? HeaderExpression { get; set; }

    /// <summary>
    ///     字段使用的轴表达式。
    /// </summary>
    public string Axis { get; set; } = string.Empty;

    /// <summary>
    ///     字段使用的筛选器表达式。
    /// </summary>
    public string Filter { get; set; } = string.Empty;

    /// <summary>
    ///     固定位置的码号名列表，只有在<see cref="Axis" />未配置时才有用。
    /// </summary>
    public List<string> FixedCodes { get; set; } = [];

    /// <summary>
    ///     忽略的码号。
    /// </summary>
    public List<string> IgnoreCodes { get; set; } = [];

    /// <summary>
    ///     是否移除空白行。
    /// </summary>
    public bool RemoveEmptyRow { get; set; }

    /// <summary>
    ///     字段单独指定的排序方式，如果不是<see cref="SortOrders.None" />，
    ///     此值优先于<see cref="PreTableOptions.SortOrder" />属性值。
    /// </summary>
    public SortOrders SortOrder { get; set; } = SortOrders.None;
}


[Serializable]
public class PreTableOptions
{
    private bool _isRootChecked;

    /// <summary>
    ///     项目根目录。
    /// </summary>
    [ArgumentProperty("-root")]
    public string Root { get; set; } = ".";

    /// <summary>
    ///     是否保留生成的文件。
    /// </summary>
    [ArgumentProperty("-keep")]
    public bool Keep { get; set; }

    /// <summary>
    ///     是否打印控制台输出内容。
    /// </summary>
    [ArgumentProperty("-p")]
    public bool Print { get; set; }

    /// <summary>
    ///     元数据文件相对根目录的路径。
    /// </summary>
    [ArgumentProperty("-metadata")]
    public string Metadata { get; set; } = string.Empty;

    /// <summary>
    ///     数据库文件相对根目录的路径。
    /// </summary>
    [ArgumentProperty("-casedata")]
    public string CaseData { get; set; } = string.Empty;

    /// <summary>
    ///     表格全局排序方式。
    /// </summary>
    [ArgumentProperty("-order")]
    public SortOrders SortOrder { get; set; } = SortOrders.None;

    /// <summary>
    ///     文件替换表，Key是模板文件，Value是生成文件。
    /// </summary>
    [ArgumentProperty("-temps", IsDictionaryKey = true)]
    [ArgumentProperty("-files", IsDictionaryValue = true)]
    public Dictionary<string, string> Files { get; set; } = [];

    /// <summary>
    ///     表头使用的筛选条件。
    /// </summary>
    [ArgumentProperty("-header")]
    public string HeaderExpression { get; set; } = string.Empty;

    /// <summary>
    ///     字段配置集合。
    /// </summary>
    [ArgumentProperty("-optnames", PropertyBinding = nameof(PreFieldOptions.Name))]
    [ArgumentProperty("-fnames", PropertyBinding = nameof(PreFieldOptions.FieldName), CanFollow = true)]
    [ArgumentProperty("-fsort", PropertyBinding = nameof(PreFieldOptions.SortOrder), CanFollow = true)]
    [ArgumentProperty("-fnumcode", PropertyBinding = nameof(PreFieldOptions.NumberCode), CanFollow = true)]
    [ArgumentProperty("-fixed", PropertyBinding = nameof(PreFieldOptions.FixedCodes), Separator = ",")]
    [ArgumentProperty("-fignore", PropertyBinding = nameof(PreFieldOptions.IgnoreCodes), Separator = ",")]
    [ArgumentProperty("-ffilter", PropertyBinding = nameof(PreFieldOptions.Filter))]
    [ArgumentProperty("-frer", PropertyBinding = nameof(PreFieldOptions.RemoveEmptyRow))]
    [ArgumentProperty("-faxis", PropertyBinding = nameof(PreFieldOptions.Axis))]
    [ArgumentProperty("-fheader", PropertyBinding = nameof(PreFieldOptions.HeaderExpression))]
    public List<PreFieldOptions> Fields { get; set; } = [];

    /// <summary>
    ///     修改MDM文档使用的文件相对路径，可选，忽略时设置成空字符串。
    /// </summary>
    [ArgumentProperty("-manip")]
    public string Manipulation { get; set; } = string.Empty;

    /// <summary>
    ///     额外依赖的文件路径，可选。
    /// </summary>
    [ArgumentProperty("-rinc")]
    public List<string> RenderIncludes { get; set; } = [];

    /// <summary>
    ///     渲染表格时需要额外引入的字段定义文件，可选。
    /// </summary>
    [ArgumentProperty("-finc")]
    public List<string> FieldIncludes { get; set; } = [];

    /// <summary>
    ///     文件编码格式，默认是中文编码。
    /// </summary>
    [ArgumentProperty("-encoding")]
    public string Encoding { get; set; } = "GB2312";

    /// <summary>
    ///     全局筛选器
    /// </summary>
    [ArgumentProperty("-filter")]
    public string GlobalFilter { get; set; } = string.Empty;


    /// <summary>
    ///     获取绝对路径
    /// </summary>
    public string AsAbsolutePath(string path)
    {
        if (!_isRootChecked)
        {
            if (Root == ".")
            {
                Root = Directory.GetCurrentDirectory();
            }
            else if (!Path.IsPathFullyQualified(Root))
            {
                Root = Path.Combine(Directory.GetCurrentDirectory(), Root);
            }

            _isRootChecked = true;
        }

        if (Path.IsPathFullyQualified(path))
        {
            return path;
        }

        return Path.GetFullPath(path, Root);
    }
}